// ==UserScript==
// @name         miniSearch
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  搜不到我也没办法
// @author       LinHQ
// @include      *
// @grant GM_xmlhttpRequest
// @grant GM_addStyle
// @connect *
// ==/UserScript==

'use strict';
(function () {
    let s = {
        // 服务器地址
        s1: '192.168.1.2:3300',
        s2: '47.103.213.130:3002',
        // 窗口透明度 0 ~ 1
        opacity: 0.7,
        // 初始是否隐藏
        hide: false,
        // 窗口位置，左上角为原点
        top: '20px',
        left: '20px',
        // 窗口的高和宽
        height: '15vw',
        width: '20vw'
    }
    let g_server = s.s2;

    // Create box
    let g_box = document.createElement("div");
    g_box.id = 'box';
    g_box.style.opacity = (s.hide) ? 0 : s.opacity;
    document.body.append(g_box);

    // Add styles
    GM_addStyle(`#box {height: ${s.height}; width: ${s.width}; position: fixed; left: ${s.left}; top: ${s.top}; z-index: 1000; background: #e5e5e5; overflow: auto; border-radius: 5px}`);
    GM_addStyle('#box {display: flex; flex-flow: column; justify-content: flex-start}');
    GM_addStyle("::selection {background: #F2F4F5}");

    document.body.onkeydown = event => {
        if (event.key == 'h' && event.altKey) {
            if (s.hide) {
                g_box.style.opacity = s.opacity;
                s.hide = false;
            } else {
                g_box.style.opacity = 0;
                s.hide = true;
            }
        } else if (event.key == 'q' && event.altKey) {
            let selected = window.getSelection().toString();

            if (selected.length < 3) {
                let warning = document.createElement("h6");
                warning.append(document.createTextNode("字数太少了..."));
                warning.style.fontSize = '15px';
                warning.style.margin = '2px';
                warning.style.color = 'red';
                g_box.append(warning);
                return;
            }

            GM_xmlhttpRequest({
                method: 'GET',
                url: `http://${g_server}/query?title=${encodeURIComponent(selected)}`,
                onload: res => {
                    let data = JSON.parse(res.responseText).answers;
                    // 擦除掉上次结果
                    g_box.innerHTML = '<h5 style="color: grey; margin: 5px 5px; font-size: 15px">查询结果如下: </h5>';
                    // 遍历结果集，增加结果结点
                    for (let e of data) {
                        if (e.title == 'Error_i') continue;
                        let ibox = document.createElement("div");
                        ibox.className = 'ibox';
                        ibox.style.margin = '5px 5px';
                        ibox.innerHTML = "<p style='font-size: 12px; font-weight: normal; margin: 0'>"
                            + `<span style='font-style: italic; color: green'>[${e.from}]</span>`
                            + `<br>${e.title}<br>`
                            + `<span style='font-size:14px; font-weight: bold;'>> ${e.answer}</span>`
                            + '</p>';
                        g_box.append(ibox);
                    }
                },
                onerror: err => console.log(err)
            })
        } else if (event.altKey && /[1-2]/.test(event.key)) {
            g_server = s[`s${event.key}`];
            console.log(g_server);
        }
    }
})();